import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

export default {
  namespace: 'message',
  state: {
    listData: [], // 列表数据
    limit: 8,
    offset: 0,
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    }
  },
  effects: {
    // 获取列表数据
    * loadList({payload = {}}, {select, put, call}) {
      const {limit, offset} = yield select(({message}) => message)
      const param = {
        limit, offset
      }
      const {data} = yield call(request, {
        url: SERVERURL + '/service/workshop/getPage',
        data: param,
        method: 'POST',
      })

      if (data && data === "unauthc") {
        Taro.navigateTo({
          url: `/pages/login/index`
        })
        Taro.showToast({
          title: ("会话过时"),
          icon: 'none'
        })
      }

      if (data && data.rows instanceof Array) {
        const d = data.rows || {};
        yield put({
          type: 'updateState',
          payload: {
            listData: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    }
  }
};
